const remote = require('@electron/remote')

const Menu = remote.Menu
let builder = new Menu()

export const tmplOpts = [
  {
    label: '点击',
    click () {
      console.log('item 1 clicked')
    }
  },
  {
    type: 'separator'
  },
  {
    label: '复制',
    role: 'copy',
    accelerator: 'CmdOrCtrl+c'
  },
  {
    type: 'separator'
  },
  {
    label: '粘贴',
    role: 'paste',
    accelerator: 'CmdOrCtrl+v'
  },
  {
    type: 'separator'
  },
  {
    label: '刷新',
    role: 'reload',
    accelerator: 'CmdOrCtrl+r'
  },
  {
    type: 'separator'
  },
  {
    label: '控制台',
    role: 'toggleDevTools',
    click () {
      console.log('toggleDevTools')
    }
  }
]

const toolsPanel = new Map([
  ['getSelection', Menu.buildFromTemplate([{
    label: '复制',
    role: 'copy',
    accelerator: 'CmdOrCtrl+c'
  },
  {
    type: 'separator'
  },
  {
    label: '刷新',
    role: 'reload',
    accelerator: 'CmdOrCtrl+r'
  }])]
])

export function reCreateMenu (panel: string) {
  if (toolsPanel.has(panel)) {
    toolsPanel.get(panel).popup({
      window: remote.getCurrentWindow()
    })
  }
}

export function initOptsMenu (): MenuOpts {
  builder = Menu.buildFromTemplate(tmplOpts)

  return {
    listener (e: Event) {
      e.preventDefault()
      builder.popup({
        window: remote.getCurrentWindow()
      })
    },
    updateOpts (optios: any[] = tmplOpts) {
      if (optios && optios.length > 0) {
        builder = Menu.buildFromTemplate(optios)
      }
    }
  }
}
